﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 网络消息的总调度
/// </summary>
public class EventManager : MonoBehaviour
{

    private void Start()
    {
        NetworkManager.Instance.networkEvent += OnMessage;
    }
    private void OnDestroy()
    {
        NetworkManager.Instance.networkEvent -= OnMessage;
    }

    void OnMessage(ByteBuffer buffer)
    {
        //处理消息
        int protocol = buffer.ReadInt();
        string msg = buffer.ReadStringOnly();
        Debug.Log("协议" + protocol + ";消息体：" + msg);

        switch (protocol)
        {
            case Protocol.Login_CMD://别人登录成功
                Debug.Log("登录成功");
                LoginPanel.loginSuccess(msg, false);
                break;
            case Protocol.SelfLogin_CMD://自己登录成功
                Debug.Log("登录成功");
                LoginPanel.loginSuccess(msg, true);
                break;
            case Protocol.PlayerPosition_CMD:
                Debug.Log("老用户更新");
                UserData user = JsonUtility.FromJson<UserData>(msg);
                GameManager.playerPosition(user);
                break;
            case Protocol.Register_CMD:
                Debug.Log("注册成功");

                break;
            case Protocol.PlayerMove_CMD://玩家移动消息
                GameManager.playerMove(msg);
                break;
            case Protocol.PlayerRota_CMD:
                GameManager.playerRota(msg);
                break;
            case Protocol.PlayerFire_CMD:
                GameManager.playerFire(msg);
                break;
            case Protocol.PlayerLogOut:
                Debug.Log("有人退出");
                User user1 = LitJson.JsonMapper.ToObject<User>(msg);
                GameManager.playerLogOut(user1);
                break;
            default:
                break;
        }
    }
}
